package view.controller;

import common.Message;
import common.NowUser;
import common.User;
import dao.CheckUser;
import dao.GetUser;
import dao.checklogin;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import server.ChatThread;
import server.ClientConnServer;
import utils.MD5;
import view.FindBackPsw;
import view.FriendList;
import view.Register;

import javax.swing.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class Login {
    public TextField account;
    public PasswordField password;
    public Button registerbt;
    public Button fpwdbt;
    public Button loginbt;

    public void loginbt() throws IOException {
        User user = new User();
        user.setId(account.getText());
        if(account.getText().equals("")){
            JOptionPane.showMessageDialog(null, "账号不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(password.getText().equals("")){
            JOptionPane.showMessageDialog(null, "密码不能为空！", "提示", JOptionPane.ERROR_MESSAGE);
        }else if(!new checklogin().checkLogin(user,password.getText())){
            JOptionPane.showMessageDialog(null, "账号或密码错误！", "提示", JOptionPane.ERROR_MESSAGE);
        }else{
            JOptionPane.showMessageDialog(null, "登陆成功", "提示", JOptionPane.PLAIN_MESSAGE);
//            User user = new User();
            MD5 md5 = new MD5();
            user.setPsw(md5.generateMD5(password.getText()));
//            try {
//                boolean flag = new CheckUser().checkUser(user);
//                if(flag){
//                    System.out.println("可登录");
//                    Message message = new Message();
//                    message.setType("getuser");
//                    message.setUser(user);
                    User user1 = new GetUser().getUser(user);
                    user1.setState(1);
                    System.out.println(user1.toString());
                    NowUser.setUser(user1);
                    CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
                    NowUser.cyclicBarrier = cyclicBarrier;
                    Stage stage = (Stage)loginbt.getScene().getWindow();
                    stage.close();
                    ChatThread chatThread = new ChatThread(NowUser.socket);
                    NowUser.chatThread = chatThread;
                    chatThread.start();
                    FriendList friendList = new FriendList();
                    Stage stage1 = new Stage();
                    try{
                        friendList.start(stage1);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                }else {
//                    System.out.println("登录失败");
//                    JOptionPane.showMessageDialog(null, "登录失败！", "提示", JOptionPane.ERROR_MESSAGE);
//                }
//            } catch (ClassNotFoundException e) {
//                e.printStackTrace();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//            Message msg = new Message();
//            msg.setUser(user);
//            msg.setType("login");

        }
    }

    public void tofpwd() throws IOException{
        Stage stage = (Stage)fpwdbt.getScene().getWindow();
        stage.close();
        FindBackPsw findBackPsw = new FindBackPsw();
        findBackPsw.start(stage);
    }

    public void toregister() throws IOException{
        Stage stage = (Stage)registerbt.getScene().getWindow();
        stage.close();
        Register register = new Register();
        register.start(stage);
    }
}
